﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AtomicTypes;
using NDimArrayNamespace;

namespace QLabValues
{
    public class QCharNDimArray : NDimArray<AtomicTypes.QChar>, IQLabValue, IIsComplex
    {
        public QCharNDimArray()
            : base()
        {
        }

        public QCharNDimArray(string s)
            : base(s.Select(t => new QChar(t)).ToArray(), s.Length)
        {

        }

        public QCharNDimArray(params int[] dimensionSize)
            : base(dimensionSize)
        {
        }
        public QCharNDimArray(QChar[] data, params int[] dimensionSize)
            : base(data, dimensionSize)
        {
        }

        //Darko dodao
        public QCharNDimArray(QCharNDimArray qca)
            : base(qca.Data, qca.DimensionSize)
        {
        }

       //Darko ubacio
        public QCharNDimArray(QLabValues.IQLabValue iqv)
        {
            if (iqv.GetEnumType() == QLabValues.IQLabValueType.QCharNDimArray)
            {
                QCharNDimArray qca = new QCharNDimArray((QCharNDimArray)iqv);
                QChar[] els = new QChar[qca.Data.Count()];
                for (int i = 0; i < qca.Data.Count(); i++)
                    els[i] = new QChar(qca.Data[i]);
                this.Data = (QChar[])els.Clone();
                this.DimensionSize = (int[])qca.DimensionSize.Clone();
            }
            else
            {
                //do ovog slucaja verovatno nikad nece doci zato sto ce pri koriscenju ovog konstruktora
                //u NumericalCalculations projectu, uvek pre njegovog pozivanja biti pozvana funkcija
                //canConvertListToEnum koja ce proveravati da li dobijeni IQlabValue moze da se konvertuje u ovaj tip
                //i ukoliko ona vrati true, tada ce se konstruktor pozvati
                throw(new Exception("Cannot convert this type to QCharArray."));
            }
        }

        public int[] ToIndexArray()
        {
            List<int> temp = new List<int>();
            for (int i = 0; i < this.Data.Count(); i++)
                temp.Add(Convert.ToInt32(Data[i].Real) - 1);
            return temp.ToArray();
        }

        public QCharNDimArray GetSubArray(int[][] subNDimArrayCoordinates)
        {
            NDimArray<QChar> tempArray = base.GetSubNDimArray(subNDimArrayCoordinates);
            return new QCharNDimArray(tempArray.Data, tempArray.DimensionSize);
        }

        public object Clone()
        {
            return (object)new QCharNDimArray(Data, DimensionSize); 
        }

        public QCharNDimArray SubAssignment(QCharNDimArray matrix, int[][] subNDimArrayCoordinates)
        {
            NDimArray<QChar> tempArray = base.SubscriptedAssignment(new NDimArray<QChar>(matrix.Data, matrix.DimensionSize), subNDimArrayCoordinates);
            return new QCharNDimArray(tempArray.Data, tempArray.DimensionSize);
        }
        public QLabValues.IQLabValueType GetEnumType()
        {
            return QLabValues.IQLabValueType.QCharNDimArray;
        }

        public bool LogicalValue()
        {
            for (int i = 0; i < Data.Count(); i++)
                if (Data[i].Real==0)
                    return false;
            return true;
        }

        public override string ToString()
        {
            //string str = base.ToString();
            //int i = 0;
            //while (i < str.Length)
            //{
            //    switch (str[i])
            //    {
            //        case ',':
            //        case '[':
            //            str = str.Remove(i, 1);
            //            break;
            //        case ']':
            //            if (str[i - 1] != '\n')
            //            {
            //                str = str.Remove(i, 1);
            //                str = str.Insert(i, "\n");
            //                i++;
            //            }
            //            else
            //                str = str.Remove(i, 1);
            //            break;
            //        default:
            //            i++;
            //            break;
            //    }

            //}
            //return str;
            StringBuilder sb = new StringBuilder();
            for(int i = 0; i < this.DimensionSize[0]; i++)
            {
                 sb.Append((char)this[i].Real);
            }
            return sb.ToString();
        }
    }
}
